#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

########### library variables ##########
# URI=https://github.com/OSGeo/gdal/releases/download/v3.4.1/gdal-3.4.1.tar.gz
GIT=https://gitee.com/springstudio/gdal.git
COMMIT=v3.4.1
LOCAL=gdal

########### build library ##########

build_arch()
{
  cd gdal
  ./autogen.sh

  # rm -rf build
  # mkdir build
  # cd build

  # export LDFLAGS=-L$SYSROOT/usr/lib/$TARGET
  # export CFLAGS="-I$SYSROOT/usr/include "$CFLAGS
  export LDFLAGS="-L$PREFIX/lib -Wl,-rpath-link=$PREFIX/lib:$SYSROOT/usr/lib/$TOOL_PREFIX/$API"

  # From GDAL 3.5, an experimental CMake-based build is available.
  # cmake \
  #   -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
  #   -DCMAKE_INSTALL_PREFIX=$PREFIX \
  #   -DCMAKE_BUILD_TYPE=Release \
  #   -DANDROID_PLATFORM=$API \
  #   -DANDROID_ABI=$ABI \
  #   -DANDROID_NDK=$ANDROID_NDK \
  #   -DANDROID_TOOLCHAIN=clang \
  #   -DBUILD_SHARED_LIBS=ON \
  #   ..
  
  # 64 bit IO is only available on 32-bit android since API 24 / Android 7.0
  if [[ $API < 24 ]]; then
    b64=no
  else
    b64=yes
  fi

  ./configure \
    --prefix=$PREFIX \
    --host=$TARGET \
    --with-sysroot=$SYSROOT \
    --with-unix-stdio-64=$b64 \
    --with-threads \
    --disable-static \
    --with-geotiff=internal \
    --with-libtiff=internal \
    --with-jpeg=internal \
    --with-libz=internal \
    $ARG

  make clean
  make -j4 -s
  make install

  build_jni
}

build_jni()
{
  SWIG=$BUILD_ROOT/gdal/gdal/swig

  # remove file with error code
  rm -rf $SWIG/java/apps/GDALtest.java

  cd $SWIG
  make BINDINGS=java veryclean
  make BINDINGS=java ANDROID=yes build

  rm -rf $SWIG/android
  mkdir $SWIG/android
  cd $SWIG/android

  mkdir jni
  cp $SWIG/java/*_wrap.cpp jni
  cp $SWIG/java/gdalconst_wrap.c jni

  mkdir jniwrap
  cp -r $SWIG/java/org jniwrap/

  cd jni

  if [ -f $PREFIX/lib/libgdal.so ]; then
      echo "LOCAL_PATH := \$(call my-dir)" >Android.mk
      # import libgdal.so
      echo "include \$(CLEAR_VARS)" >>Android.mk
      echo "LOCAL_MODULE := gdal" >>Android.mk
      echo "LOCAL_SRC_FILES := $PREFIX/lib/libgdal.so" >>Android.mk
      echo "LOCAL_EXPORT_C_INCLUDES := $PREFIX/include" >>Android.mk
      echo "TARGET_ARCH_ABI := $ABI" >>Android.mk
      echo "include \$(PREBUILT_SHARED_LIBRARY)" >>Android.mk
      # Android.mk
      echo "include \$(CLEAR_VARS)" >>Android.mk
      echo "LOCAL_MODULE := gdalalljni" >>Android.mk
      echo "LOCAL_SRC_FILES := gdal_wrap.cpp osr_wrap.cpp gdalconst_wrap.c ogr_wrap.cpp" >>Android.mk
      echo "LOCAL_SHARED_LIBRARIES := gdal" >>Android.mk
      echo "include \$(BUILD_SHARED_LIBRARY)" >>Android.mk
      # Application.mk
      echo "APP_STL := c++_static" >Application.mk
      echo "APP_CFLAGS := Android.mk" >>Application.mk
      echo "APP_ABI := $ABI" >>Application.mk
      echo "APP_PLATFORM := android-$API" >>Application.mk
      echo "APP_OPTIM := release" >>Application.mk
      ndk-build
      cp $SWIG/java/gdal.jar $PREFIX/
      cp $SWIG/android/libs/$ABI/*.so $PREFIX/lib/
  fi
}

clear_arch() {
  if [ -d build ]; then
    cd build && make clean && cd -
    rm -rf build
  fi
}

PS3='Enter your compile options: '
options=("lite" "lite-with-proj" "default")
select opt in "${options[@]}"
do
    case $opt in
        "lite")
            ARG=
            break
            ;;
        "lite-with-proj")
            ARG="--with-proj=$PREFIX"
            break
            ;;
        "default")
            ARG="--with-proj=$PREFIX --with-expat=$PREFIX --with-libkml=$PREFIX --with-geos=$PREFIX/bin/geos-config"
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

############# forbid modify ###########

. ./tools/do-compile.sh
